#include "StdAfx.h"
#include "SubStructure.h"
#include "Enemy.h"
#include "Mediator.h"
#include "JacoEnemy.h"

JacoEnemy::JacoEnemy()
{
	Init();
}

JacoEnemy::~JacoEnemy()
{
}

void JacoEnemy::Init()
{
	
}

void JacoEnemy::Move()
{
	pos += now_move.Move_Vel;
	now_move.Move_Vel += now_move.Move_Acc;
	move_count++;
	if(move_count > now_move.move_count)
	{
		if(nowMoveState2 >= (_Move[nowMoveState1].size() - 1))
		{
			nowMoveState1 = now_move.next_Move;
			nowMoveState2 = 0;
			now_move = *_Move[nowMoveState1][nowMoveState2];
		}
		else
		{
			++nowMoveState2;
			now_move = *_Move[nowMoveState1][nowMoveState2];
		}
		move_count = 0;
	}
}

void JacoEnemy::Update()
{
	live_time++;

	ani_count++;
	if( now_move.Move_Vel.x > abs(now_move.Move_Vel.y) && (nowAniState1 != 1 && nowAniState1 != 2))
	{
		nowAniState1 = LEFTREADY;
		nowAniState2 = 0;
		now_ani = *_Ani[nowAniState1][nowAniState2];
	}
	else if( abs(now_move.Move_Vel.x) > abs(now_move.Move_Vel.y) && (nowAniState1 != 3 && nowAniState1 != 4))
	{
		nowAniState1 = RIGHTREADY;
		nowAniState2 = 0;
		now_ani = *_Ani[nowAniState1][nowAniState2];
	}

	if(ani_count >= now_ani.this_cut_count)
	{
		ani_count = 0;
		if(nowAniState2 >= (_Ani[nowAniState1].size() - 1))
		{
			nowAniState1 = now_ani.next_ani;
			nowAniState2 = 0;
			now_ani = *_Ani[nowAniState1][nowAniState2];
		}
		else
		{
			++nowAniState2;
			now_ani = *_Ani[nowAniState1][nowAniState2];
		}
	}

	EventList::iterator iter = makePattern.begin();
	EventList::iterator iterend = makePattern.end();
	EventList templist;
	D3DXVECTOR2 center((now_ani.Source.bottom - now_ani.Source.top)/2.0f, (now_ani.Source.right - now_ani.Source.left)/2.0f);
	for(;iter != iterend; iter++)
	{
		if((*iter)->sub2 == 0)
		{
			if((*iter)->DoTime == live_time)
			{
				templist.push_back(*iter);
				(*iter)->makePos = pos + center;
			}
		}
		else if((*iter)->sub2 == 1)
		{
			int i = (*iter)->sub2;
			int k = (*iter)->sub1;
			for(int j = i; j <= live_time; j = j + k)
			{
				if(j == live_time)
				{
					templist.push_back(*iter);
					(*iter)->makePos = pos + center;
				}
			}
		}
		else
		{
			if((*iter)->sub2 < 16)
			{
			}
			else if((live_time - (*iter)->DoTime) % (*iter)->sub1 == 0)
			{
				templist.push_back(*iter);
				(*iter)->makePos = pos + center;
				(*iter)->sub2 -= 16;
			}
		}
	}

	processor->GetEvent(templist);
}

void JacoEnemy::Draw()
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	D3DXMatrixTransformation2D(&matrix, 0, 0, 0, 0, 0, &pos);

	GameBase::pSprite->SetTransform(&matrix);
	GameBase::pSprite->Draw(now_ani.Texture,
							&now_ani.Source,
							NULL,
							NULL,
							now_ani.invisible);
	D3DXMatrixIdentity(&matrix);
	GameBase::pSprite->SetTransform(&matrix);
}

DWORD JacoEnemy::deleteCheck()
{
	if(pos.x < -10.0 || pos.y < -26.0 || pos.x > 420.0 || pos.y > 510.0)
	{
		return OVERRANGEDELETE;
	}
	else if(live_time >= limit_time)
	{
		return TIMEOVERDELETE;
	}
	return 0;
}